using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get;private set; }
    
    [SerializeField]private int score=0;//得分
    [SerializeField]private int level = 1;//关卡
    [SerializeField]private int lives = 3;//生命数

    public int Score
    {
        get => score;
        set
        {
            score = value;
            onScoreChanged?.Invoke(value);
        }
    }

    public int Lives
    {
        get => lives;
        set
        {
            lives = value;
            onLivesChanged?.Invoke(value);
        }
    }

    public event Action<int> onScoreChanged; 
    public event Action<int> onLivesChanged; 
    
    private Ball ball;
    private Paddle paddle;
    private Brick[] bricks;
    
    
    private void Awake()
    {
        if(Instance!=null){
            DestroyImmediate(this.gameObject);
        }
        else
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);
            SceneManager.sceneLoaded += OnLevelLoaded;
            
        }
    }

    /// <summary>
    /// 查找引用
    /// </summary>
    private void FindReferences()
    {
        ball = FindObjectOfType<Ball>();
        paddle = FindObjectOfType<Paddle>();
        bricks = FindObjectsOfType<Brick>();
        
    }

    private void Start()
    {
       // NewGame();
    }

    public void NewGame()
    {
        //初始化
        this.Score = 0;
        this.Lives = 3;
        //加载场景
        LoadLevel(1);
    }

    private void LoadLevel(int level)
    {
        this.level = level;//GameManager全局level=传入的参数
        
        //按照名字加载不容易出错
        SceneManager.LoadScene("Level" + level);
    }

    /// <summary>
    /// 关卡加载时
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="mode"></param>
    private void OnLevelLoaded(Scene scene,LoadSceneMode mode)
    {
        FindReferences();
    }

    /// <summary>
    /// 击中砖块
    /// </summary>
    /// <param name="brick"></param>
    public void OnBrickHit(Brick brick)
    {
        //增加分数
        Score += brick.point;
        if (IsClear())
        {
            LoadLevel(level+1);
        }
    }

    /// <summary>
    /// 是否清空砖块
    /// </summary>
    /// <returns></returns>
    private bool IsClear()
    {
        for(int i=0; i < bricks.Length;i++){
            if (bricks[i].gameObject.activeInHierarchy && !bricks[i].unbreakable)
            {
                return false;
            }
        }

        return true;
    }

    /// <summary>
    /// 球掉地上
    /// </summary>
    public void Miss()
    {
        //判断是否是最后一个球，是最后一个球再减生命值
        if (!IsLastBall()) return;
        this.Lives--;
        
        if (Lives>0)
        {
            ResetLevel();
        }
        else
        {
            GameOver();
        }
    }

    /// <summary>
    /// 是否为最后一个球
    /// </summary>
    /// <returns></returns>
    public bool IsLastBall()
    {
       Ball[] balls = FindObjectsOfType<Ball>();
       return balls.Length == 1;
    }

    public void IncreaseLives()
    {
        this.Lives++;
    }

    private void ResetLevel()
    {
        //重置球和球拍
        this.ball.RestBall();
        this.paddle.RestPaddle();
        /*for (int i = 0; i < bricks.Length; i++)
        {
            this.bricks[i].RestBrick();
        }*/
        
    }

    private void GameOver()
    {
        NewGame();
    }
}
